package g16.keephealthy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class EstatisticasActivity extends Activity {

	//variaveis da actividade necessarias
	Button del;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_estatisticas);
        
        
        del = (Button) findViewById(R.id.del1);
        String file = "historico.txt";
        File files = getBaseContext().getFileStreamPath(file);
        
        //verifica se o ficheiro existe
        if(!files.exists())
    	{
        	String erro="Nao foi realizado nenhum exercicio!!";
    		TextView historico = (TextView) findViewById(R.id.historico);
			historico.setText(erro);
			return;
    	}
        
        //se ficheiro existir faz o seguinte
        try {
        	
        	BufferedReader br = new BufferedReader(new FileReader(files));
        	String line1="", line;
        	
        	if(files.exists())
        	{  
        		try {
        			//escreve tudo o que esta presente no ficheiro para a caixa de texto
					while((line = br.readLine()) != null)
					{
						line1+=line+"\n";
					}
					
					TextView historico = (TextView) findViewById(R.id.historico);
					historico.setText(line1);
					historico.setMovementMethod(new ScrollingMovementMethod());
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
        	}else
        	{
        		//erro na abertura do ficheiro
        		String erro="Erro na abertura do ficheiro ou nenhum exercicio realizado!";
        		TextView historico = (TextView) findViewById(R.id.historico);
				historico.setText(erro);
        		
        	}
        	
        	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        //on click no botao de apagar historico apaga o ficheiro historico.txt
        del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			     String filehist = "historico.txt";   
			     File filehistF = getBaseContext().getFileStreamPath(filehist);
			     			    
			     if(filehistF.exists())
			    	 filehistF.delete();
			     
			     finish();
     	         startActivity(getIntent()); 
			 
			}
		});
        
        
        
        
    }

    
    
    
    @Override
    public void onBackPressed() {
        
	    Intent VoltaMenu = new 
		Intent(EstatisticasActivity.this,MainActivity.class);
	    EstatisticasActivity.this.startActivity(VoltaMenu);
	    EstatisticasActivity.this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
